#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;

#define endl '\n'

ll a[200005];

int main(){
    freopen("color.in", "r", stdin);
    #ifndef DEBUG
    freopen("color.out", "w", stdout);
    #endif
    cin.tie(0)->sync_with_stdio(0);
    cout.tie(0);
    int T;
    cin>>T;
    while(T--){
        int n;
        map<int,ll> m;
        cin>>n;
        for(int i=1;i<=n;i++){
            cin>>a[i];
            m[a[i]]++;
        }
        ll an=0;
        if(n<=24){
            for(ull k=0;k<((1ULL)<<(n-1));k++){
                ll ans=0;
                int lasta=-1;
                int lastb=-1;
                for(int i=1;i<=n;i++){
                    // cerr<<int((k>>(i-1))&1);
                    if((k>>(i-1))&1){
                        if(lasta == a[i]){
                            ans+=a[i];
                        }
                        else lasta = a[i];
                    }
                    else {
                        if(lastb == a[i]){
                            ans+=a[i];
                        }
                        else lastb=a[i];
                    }
                }
                // cerr<<endl;
                an=max(ans,an);
            }
        }
        else {
            for(auto&x:m){
                const int b=x.first;
                ll ans=ll(x.first)*(x.second-1);
                int last=-1;
                for(int i=1;i<=n;i++){
                    if(a[i]!=b){
                        if(last == a[i]){
                            ans+=a[i];
                        }
                        else last=a[i];
                    }
                }
                an=max(ans,an);
            }
        }
        cout<<an<<endl;
    }
    return 0;
}